<template>
    <div id="addGoods">
        <Separator :nameList='nameList'></Separator>
        <el-card>
            <!-- //提示区域 -->
            <el-alert title="添加商品信息" type="warning" show-icon :closable='false' center></el-alert>
            <el-steps align-center :active="activeIndex -0 " finish-status="success">
                 <el-step title="基本信息"></el-step>
                 <el-step title="商品参数"></el-step>
                 <el-step title="商品属性"></el-step>
                 <el-step title="商品图片"></el-step>
                 <el-step title="商品内容"></el-step>
                 <el-step title="完成"></el-step>
            </el-steps>
            <el-form :model="AddGoodsForm" :rules="AddGoodsrules" ref="AddGoodsRefs" label-position='top' class="demo-ruleForm">
                <el-tabs v-model="activeIndex" tab-position="left" @tab-click='tbaClick' :before-leave='changeTab'>
                    <el-tab-pane label="基本信息" name='0'>
                        <el-form-item label="商品名称"  prop="goods_name">
                            <el-input v-model="AddGoodsForm.goods_name"></el-input>
                        </el-form-item>
                         <el-form-item label="商品价格"  prop="goods_price">
                            <el-input v-model="AddGoodsForm.goods_price" type='number'></el-input>
                        </el-form-item>
                        <el-form-item label="商品重量"  prop="goods_weight">
                            <el-input v-model="AddGoodsForm.goods_weight"  type='number'></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量"  prop="goods_number">
                            <el-input v-model="AddGoodsForm.goods_number"  type='number'></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类" prop='CategoriesValue'>                     
                        <el-cascader  v-model="AddGoodsForm.CategoriesValue"
                                      :options="CategoriesOptions"
                                      :props="{expandTrigger: 'hover',children:'children', value:'cat_id', label:'cat_name'  }"
                                      @change="CategoriesandleChange">
                        </el-cascader>
                        </el-form-item>
                        
                    </el-tab-pane>
                    <el-tab-pane label="商品参数" name='1'>
                        <el-form-item :label="item.attr_name" v-for="item in goodsCategories" :key="item.attr_id">
                             <el-checkbox-group v-model="item.attr_vals" >
                              <el-checkbox border v-for="(element,index) in item.attr_vals" :key="index" :label="element"></el-checkbox>
                             </el-checkbox-group>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品属性" name='2'>
                        <el-form-item :label="only.attr_name" v-for="only in onlyDataList" :key="only.attr_id">
                            <el-input v-model="only.attr_vals"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品图片" name='3'>
                         <!-- action 表示图片要上传的后台地址 -->
                        <el-upload
                            class="upload-demo"  
                            action="http://timemeetyou.com:8889/api/private/v1/upload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :headers="headersObject"
                            :on-success="headersSuccess"
                            list-type="picture">
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                            </el-upload>
                    </el-tab-pane>
                    <el-tab-pane label="商品内容" name='4'>
                        <quill-editor v-model="AddGoodsForm.goods_introduce"></quill-editor>
                         <el-button type="primary" class="btn-add" @click="addListGoods">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form> 
        </el-card>
        <!-- 图片预览 -->
        <el-dialog  title="图片预览"  :visible.sync="dialogVisible" width="30%" >
            <span><img :src="previewPicture" /></span>
        </el-dialog>
    </div>
</template>

<script>
  import Separator from 'components/content/separator/Separator' 
  import {getGoodsPrd} from 'network/goods/categories'
  import {changeGoodsCs} from 'network/goods/goods'
  export default {
   name:'AddGoods' ,
   components:{
       Separator
   },
   data(){
       return{
           nameList:['商品管理','添加商品'],
           activeIndex:'0',
           AddGoodsForm:{
               goodsName:'',
               goods_price:0,
               goods_weight:0,
               goods_number:0,
               //保存商品分类 选择的值 
               CategoriesValue:[],
               pics:[],
               goods_introduce:''

           },
            headersObject:{
                Authorization: window.sessionStorage.getItem("token")     
            },
           AddGoodsrules:{
               goods_name:[
                   {required:true,message:'请输入商品名称',trigger:'blur'},
                   {min:3,max:16,trigger:'blur'}
               ],
               goods_price:[
                   {required:true,message:'请输入商品价格',trigger:'blur'},                
               ],
                goods_weight:[
                   {required:true,message:'请输入商品重量',trigger:'blur'},                 
               ],
               goods_number:[
                   {required:true,message:'请输入商品数量',trigger:'blur'},      
               ],
               CategoriesValue:[
                  {required:true,message:'请选择商品分类',trigger:'blur'},   
               ]
           },
        
           //获取到的数据
           CategoriesOptions:[],
           //商品参数
           goodsCategories:[],
           checkList:[],
           onlyDataList:[],
           previewPicture:'',
           dialogVisible:false
       }
   },
   created(){
       this.getCategoriesList()
   },
   methods:{
       getCategoriesList(){
           getGoodsPrd(3).then(res=>{
               this.CategoriesOptions=res.data.data
               console.log(this.CategoriesOptions)
           },err=>{ 

           })
       },
        // 级联选择框值发生变化时触发
       CategoriesandleChange(){
          if(this.AddGoodsForm.CategoriesValue.length!==3){
             this.AddGoodsForm.CategoriesValue =[];
             return this.$message.error('只允许选择三级分类');
          }
       },
       changeTab(activeName, oldActiveName){
           if(oldActiveName ==='0' && this.AddGoodsForm.CategoriesValue.length!==3){
               this.$message.error('请选择三级分类');
               return false
           }
           console.log(activeName, oldActiveName);   
       },
       tbaClick(){
          if(this.activeIndex==='1'){
              const many='many';
              changeGoodsCs(this.cate_id,many).then(res=>{
               if(res.data.meta.status!==200){
                   return this.$message.error("数据列表获取失败");
               }
              res.data.data.forEach(element => {
                element.attr_vals = element.attr_vals.length===0 ? []: element.attr_vals.split(' ');
              });  
              this.goodsCategories=res.data.data;
            },err=>{
                console.log(err)
            })
          } else if(this.activeIndex==='2'){
              const only = 'only';
              changeGoodsCs(this.cate_id,only).then(res=>{
                  if(res.data.meta.status!==200){
                   return this.$message.error("数据列表获取失败");
               }
               this.onlyDataList=res.data.data;
               console.log(this.onlyDataList,11112);   
              },err=>{

              })
          }else if(this.activeIndex==='3'){
              console.log(this.headersObject);
          }   
       } ,
    //  显示图片时调用
       handlePreview(file){
           this.previewPicture=file.response.data.url;
           this.dialogVisible = true;
          console.log(file);
       },
    //  关闭图片上传弹框时调用    
       handleRemove(file){
         const filePath = file.response.data.tmp_path;
         const i = this.AddGoodsForm.pics.findIndex((x)=>{
             x.pics===filePath
         })
         this.AddGoodsForm.pics.splice(i,1);
         console.log(this.AddGoodsForm);
       } ,
    //  图片上传成功回调函数
       headersSuccess(response){
         const picInfo={pic:response.data.tmp_path};
         this.AddGoodsForm.pics.push(picInfo); 
          console.log(this.AddGoodsForm.pics); 
       },
    //    添加商品
       addListGoods(){
           this.$refs.AddGoodsRefs.validate(val=>{
               if(!val){
                   return this.$message.error('请填写必填的表单');
               }
           })
          console.log(this.AddGoodsForm);
       }
    },
    computed:{
        cate_id(){
            if(this.AddGoodsForm.CategoriesValue.length===3){
                return this.AddGoodsForm.CategoriesValue[2]
            }
            return null
        },
        // CheckboxList(){
        //    console.log(this.goodsCategories.attr_vals); 
        //    if() 
          
        // }
    }
  }
</script>

<style lang="less" scoped>
  .el-steps{
    margin-top: 15px;
  }
  .el-tabs{
      margin-top: 25px;
  }
 .el-checkbox{
     margin: 0 10px 0 0 !important;
 }
 .ql-editor{
     min-height: 300px !important;
 }
 .btn-add{
     margin-top:15px;
 }
</style>